//
//  MWDropMenuView.m
//  XiRan-iOS
//
//  Created by liuyiming on 2019/10/26.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWDropMenuView.h"
#import "MWDropMenuCell.h"

@interface MWDropMenuView () <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UIView *backgroundView;
@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) NSMutableArray *titles;
@property(nonatomic, assign) NSInteger selectedPosition;

@end

@implementation MWDropMenuView

- (instancetype)initWithFrame:(CGRect)frame {
    if (!(self =[super initWithFrame:CGRectMake(0, kNavBarHeight + 44, kScreenWidth, kScreenHeight - kNavBarHeight - 44)])) {
        return nil;
    }
    
    _isShow = NO;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    _backgroundView.backgroundColor = [UIColor blackColor];
    _backgroundView.alpha = 0.0;
    [_backgroundView addGestureRecognizer:tapGesture];
    [self addSubview:_backgroundView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self addSubview:_tableView];
    
    return self;
}

- (void)setTitleArray:(NSArray *)titleArray {
    _titleArray = titleArray;
    [self.titles removeAllObjects];
    [self.titles addObjectsFromArray:titleArray];
    [_tableView reloadData];
}

- (NSMutableArray *)titles {
    if (!_titles) {
        _titles = [[NSMutableArray alloc] init];
    }
    return _titles;
}

- (void)tapGesture:(UIGestureRecognizer *)sender {
    [self dismiss];
}

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    
    _isShow = YES;
    _backgroundView.alpha = 0.0;
    _tableView.frame = CGRectMake(0, 0, kScreenWidth, 0);
    
    MWWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.backgroundView.alpha = 0.4;
        weakSelf.tableView.frame = CGRectMake(0, 0, kScreenWidth, 350);
        
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismiss {
    _isShow = NO;
    MWWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.backgroundView.alpha = 0.0;
        weakSelf.tableView.frame = CGRectMake(0, 0, kScreenWidth, 0);
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
     return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"MWTextureCell";
    MWDropMenuCell *dropMenuCell = [_tableView dequeueReusableCellWithIdentifier:identifier];
    if (!dropMenuCell) {
        dropMenuCell = [[MWDropMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        dropMenuCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    dropMenuCell.titleLB.text = _titles[indexPath.row];
    
    if (_selectedPosition == indexPath.row) {
        dropMenuCell.stateIV.hidden = NO;
        dropMenuCell.titleLB.textColor = MWHexColor(@"#FD9F2B");
        
    } else {
        dropMenuCell.stateIV.hidden = YES;
        dropMenuCell.titleLB.textColor = MWHexColor(@"#333333");
    }
    
    return dropMenuCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_selectedPosition == indexPath.row) {
        return;
    }
    _selectedPosition = indexPath.row;
    [_tableView reloadData];
    [self dismiss];
    if ([self.delegate respondsToSelector:@selector(dropMenuView:didSelectRowAtPosition:)]) {
        [self.delegate dropMenuView:self didSelectRowAtPosition:indexPath.row];
    }
}

@end
